Optimal Control Of Spin Systems With Applications In Magnetic Resonance